﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class SeedForm : Form
    {
        public SeedForm()
        {
            InitializeComponent();
        }

        tb_Seed Seed;
        bool insert = false;
        int cultID;

        private void SeedForm_Load(object sender, EventArgs e)
        {
            cbCulture.SelectedIndexChanged -= cbCulture_SelectedIndexChanged;
            cbCulture.DataSource = UtilClass.GetCBCultureDataSourse();
            cbCulture.DisplayMember = "Name";
            cbCulture.ValueMember = "ID";
            cbCulture.SelectedIndex = -1;
            cbCulture.SelectedIndexChanged += new EventHandler(cbCulture_SelectedIndexChanged);
        }

        private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCulture.SelectedIndex >= 0)
            {
                cultID = Convert.ToInt32(cbCulture.SelectedValue);
                if (cultID > 0)
                    ShowSeeds(cultID);
            }
            else
                cultID = 0;
        }

        private void ShowSeeds(int cultID)
        {
            dgvSeed.DataSource = DBClass.Seeds
                .Where(wh => wh.CultureID == cultID)
                .Select(se => new
                {
                    ID = se.ID,
                    nameRus = se.Name,
                    nameUzb = se.NameUz
                });

            dgvSeed.Columns["ID"].Visible = false;
            dgvSeed.Columns["nameRus"].HeaderText = "Русское название";
            dgvSeed.Columns["nameUzb"].HeaderText = "Узбекское название";
            dgvSeed.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvSeed.AutoResizeColumns();
            
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (cultID <= 0)
                return;

            if (insert)
            {
                if (String.IsNullOrEmpty(tbSeedName.Text) || String.IsNullOrEmpty(tbSeedNameUzb.Text))
                {
                    MessageBox.Show("Обязательно заполнить оба названия Культуры!");
                    return;
                }

                Seed = new tb_Seed
                {
                    CultureID = cultID,
                    Name = tbSeedName.Text,
                    NameUz = tbSeedNameUzb.Text
                };
                DBClass.RESPContext.tb_Seeds.InsertOnSubmit(Seed);

                tbSeedName.Text = tbSeedNameUzb.Text = string.Empty;


            }
            else
            {
                if (Seed != null)
                {
                    Seed.Name = tbSeedName.Text;
                    Seed.NameUz = tbSeedNameUzb.Text;
                }

            }

            try
            {
                if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                ShowSeeds(cultID);
                tbSeedName.Enabled = tbSeedNameUzb.Enabled = insert = false;
                buttonOK.Text = "OK";
                buttonCancel.Text = "Cancel";
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }

        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            tbSeedName.Enabled = tbSeedNameUzb.Enabled = insert = true;
            buttonOK.Text = "Добавить";
            buttonCancel.Text = "No";
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvSeed.RowCount > 0 && dgvSeed.CurrentRow != null)
            {
                insert = false;
                tbSeedName.Enabled = tbSeedNameUzb.Enabled = true;
                int sID = (int)dgvSeed["ID", dgvSeed.CurrentRow.Index].Value;
                Seed = DBClass.Seeds.Where(wh => wh.ID == sID).SingleOrDefault();
                if (Seed != null)
                {
                    tbSeedName.Text = Seed.Name;
                    tbSeedNameUzb.Text = Seed.NameUz;
                }
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvSeed.RowCount > 0 && dgvSeed.CurrentRow != null)
            {
                int sID = (int)dgvSeed["ID", dgvSeed.CurrentRow.Index].Value;
                Seed = DBClass.Seeds.Where(wh => wh.ID == sID).SingleOrDefault();

                string cName = InformationClass.Lang ? Seed.Name : Seed.NameUz;

                string msg = InformationClass.Lang?"Вы действительно хотите удалить сорт семян \"" + cName + " \"":"Сиз ҳақиқатдан ҳам уруғлар навини олиб ташламоқчисиз \"" + cName + " \"";

                if (MessageBox.Show(msg, msgClass.msgAchtung, MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DBClass.Seeds.DeleteOnSubmit(Seed);
                    DBClass.RESPContext.SubmitChanges();
                    ShowSeeds(cultID);
                }
                else
                {
                    Seed = null;
                }

            }
        }


    }
}
